import {
    createSlice,
    createAsyncThunk
} from '@reduxjs/toolkit'

// 引入请求的的api
import {MovieRequest} from '../../api'

//创建异步操作发送请求，并且把引入中间件处理返回的reducer
export const getMovieAction= createAsyncThunk('movie/getMovieList', async()=>{
   
    //这里发送请求，并接收值
    const re = await MovieRequest()
    console.log(re,'re');

    return re.data.data.list
})


//创建reducer
const movieSlice = createSlice({
    name:'movie',

    //初始值
    initialState:{
        movieList:[]
    },
    //异步请求的reducer不写在这里，不然又会生成一个action
    reducers:{

    },

    //单独书写，异步请求的三个状态
    extraReducers:{
        [getMovieAction.fulfilled](prev,action){
            console.log(prev.movieList,'geimovielist');
            console.log('异步action成功')
            
            prev.movieList = action.payload

        },
        [getMovieAction.rejected](prev,action){
            console.log('异步action失败')
        },
        [getMovieAction.pending](prev,action){
            console.log('异步action正在进行中')
        }
    }
})

export default movieSlice.reducer


